import {getRole, setRole, removeRole} from "../../common/js/auth";
// import { resetRouter } from '../../routes'


function findRole(uType) {
    if (uType === "1"){
        return "admin";
    } else if (uType === "2"){
        return "worker";
    }else if (uType === "3"){
        return "teacher";
    } else if (uType === "14"){
        return "super_editor"
    }else if (uType === "0"){
        return "user";
    }
}

const user = {
    state: {
        // role: "",
        role: getRole(),
        name: '',
        avatar: '',
        introduction: '',
        roles: []
    },

    mutations: {
        SET_ROLE: (state, role) => {
            setRole(role)
            state.role = role
        },
        SET_INTRODUCTION: (state, introduction) => {
            state.introduction = introduction
        },
        SET_NAME: (state, name) => {
            state.name = name
        },
        SET_AVATAR: (state, avatar) => {
            state.avatar = avatar
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles
        },
    },

    actions: {
        accountRole(context, uType) {
            // console.log("accountRole--->", uType)
            return context.commit('SET_ROLE', uType)
        },
        getUserInfo({commit}, data){  // 把roles存到vuex
            return new Promise((resolve, reject) => {
                let roles = [],
                role = getRole();

                roles.push(findRole(role))
                if (roles <= 0) {
                    reject('请先登录！')
                    // reject('getUserInfo: 没有角色!')
                }else {
                    commit('SET_ROLES', roles)
                }
                resolve()
                // resolve(roles)
            })
        },



        // 退出
        LogOut({ commit, state }) {
            return new Promise((resolve, reject) => {
                commit('SET_ROLES', [])
                removeRole()
                // resetRouter()// 重置路由
                localStorage.removeItem("teacherId")
                resolve()
            })
        },

    }
}

export default user

/*export default {
    namespaced: true,
    user,
    mutations,
    actions,
}*/
